package com.foreveross.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.HouseUserDAO;
import com.foreveross.dao.HrNewsInfoDAO;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.NewsSearch;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Service("hrNewsInfoService")
@Transactional
public class HrNewsInforService extends BaseService {

	@Resource
	HrNewsInfoDAO hrNewsInfoDAO;

	@Resource
	HouseUserDAO houseUserDAO;

	public List<HrNewsInfo> findCodeList(String title, String houseId,
			String publisher, String startTime, String endTime,String type, int pageIndex,
			int pageSize,String userId) throws Exception {
		List<HrNewsInfo> listHrNewsInfo = hrNewsInfoDAO.findCodeList(title, houseId, publisher, startTime,
				endTime, type,pageIndex, pageSize,userId);
		for (HrNewsInfo hrNewsInfo : listHrNewsInfo){
			hrNewsInfo.setCreateName(hrNewsInfo.getSysUserByCreateId() == null ? "": hrNewsInfo.getSysUserByCreateId().getName());
			hrNewsInfo.setPublisherName(hrNewsInfo.getSysUserByPublisherId() == null ? "" : hrNewsInfo.getSysUserByPublisherId().getName());
		}
		return listHrNewsInfo;
	}

	public long findCodeListCount(String title, String houseId,
			String publisher, String startTime, String endTime,String type,String userId)
			throws Exception {
		return hrNewsInfoDAO.findCodeListCount(title, houseId, publisher,
				startTime, endTime,type,userId);
	}

	 

	public void savesearchKey(String weixinId, String type, String searchKey)
			throws Exception {
		NewsSearch newsSearch = new NewsSearch();
		newsSearch.setSearchKey(searchKey);
		try {
			if (type != null && !type.isEmpty())
				newsSearch.setType(Integer.parseInt(type));
		} catch (Exception e) {
		}
		newsSearch.setWeixinId(weixinId);
		newsSearch.setCreateDate(new Date());
		List<NewsSearch> list = hrNewsInfoDAO.getNewsSearch(searchKey);
		if (list != null && !list.isEmpty()) {
			NewsSearch n = list.get(0);
			n.setSearchNum(n.getSearchNum() + 1);
			hrNewsInfoDAO.updateObject(n);
		} else {
			newsSearch.setSearchNum(1);
			hrNewsInfoDAO.save(newsSearch);
		}
	}

	public List<NewsSearch> findNewsSearchList(String type, String pageSize)
			throws Exception {
		return hrNewsInfoDAO.findNewsSearchList(type, pageSize);
	}
	
	public long getTypeNewsInfoListCount(String type, String weixinId) throws Exception {
		String[] typeNames = { "新闻资讯", "新盘展示", "万象专区" };
		String typeName = type==null? null: typeNames[Integer.parseInt(type) - 1];
		long count = hrNewsInfoDAO.findTypeNewsInfoListCount(typeName, weixinId);

		return count;
	}

	public List<HrNewsInfo> getTypeNewsInfoList(String type, String weixinId, int currentPage,
			int pageSize) throws Exception {
		String[] typeNames = { "新闻资讯", "新盘展示", "万象专区" };
		String typeName = type==null? null: typeNames[Integer.parseInt(type) - 1];
		List<HrNewsInfo> hrNewsInfoList = hrNewsInfoDAO.findTypeNewsInfoList(typeName, weixinId,
				currentPage, pageSize);

		return hrNewsInfoList;
	}

	public void searchHrNews(String weixinId, String searchKey, String type,
			String currentPage, String pageSize, ResultVo resultVo)
			throws Exception {
		PageVo<HrNewsInfo> page = hrNewsInfoDAO.searchHrNews(searchKey,
				type, currentPage, pageSize);
		resultVo.setData(page);
		if (page.getData() == null || page.getData().isEmpty()) {
			resultVo.setMessage("搜索资讯内容列表为空！");
		} else {
			savesearchKey(weixinId, type, searchKey);
		}
		resultVo.setSuccess(true);
	}

}
